﻿using UnityEngine;
using System.Collections;

public enum BattleCameraState
{
    Normal,
    Follow,
    Back,
    SkillFollow,
    SkillBack,
    LookAtTarget,
    BackFromTarget,
    BackToFixed,
    MoveToNormalSkill,
}

public class BattleCameraCtrl : MonoBehaviourSingleton<BattleCameraCtrl>
{
    public static Vector3 _normalVec3 = new Vector3(50.86f, 22.32f, 0f);
    public static Vector3 _normalMoveVec3 = new Vector3(32f, 14f, 0f);

    public static Vector3 _startPosVec3 = new Vector3(21.347f, -89.10609f, 0f);
    public static Vector3 _normalEularVec3 = new Vector3(21.347f, -89.10609f, 0f);

    public BattleCameraState _battleCameraState = BattleCameraState.Normal;

    public bool _battleCameraFixed = false;

    public float troopOffset = -26f;

    public bool _moveForward;
    //public float _moveSpeed;
    public bool _canMove;
    public bool _useTimerCtrl;
    public float _finishMoveTime;
    public float _startMoveTime;

    public Camera _battleCamera;

    public Camera BattleCamera
    {
        get
        {
            if (_battleCamera == null)
            {
                BattleStage batStage = GameStageMachine.Instance.GetStage(GameStageType.Battle) as BattleStage;
                GameObject go = GameObject.Find("SceneCamera");
                if(go != null){
                    _battleCamera = go.GetComponent<Camera>();
                }
            }
            return _battleCamera;
        }
        set { _battleCamera = value; }
    }

    public void Init()
    {
       _battleCamera = null;
       float fov= GameObjectUtil.Instance.ZoomBattleCamera(BattleCamera);
    }

    public bool _intoSceneLoad = false;
    public bool _intoBattleScene = false;
    public float _intoBattleSceneSpeed;
    public float _intoBattleSceneTime = 1.0f;
    public float _intoBattleScenemFinishTime;



    //--------------------------------------------------------
    //最大最小z坐标
    public float minZ = 10f;
    public float maxZ = 16f;

    //最大最小x坐标
    public float minX = -18f;
    public float maxX = 18f;

    Vector3 _lastMouse = new Vector2();
    Vector3 _deltaMouse = new Vector2();
    Vector3 _deltaMove = new Vector3();
    public float _moveSpeed = 1;

    private bool _cameraMove = false;
    public bool bCameraMove 
    {
        get { return _cameraMove; }
        set 
        {
            _cameraMove = value;
            if (_cameraMove)
            {
                BattleCamera.transform.position = _normalMoveVec3;
            }
            else
            {
                BattleCamera.transform.position = _normalVec3;
            }
        }
    }
    //--------------------------------------------------------

    public void IntoBattleEffect()
    {
       // BattleCamera.transform.position = _startPosVec3;
       // _intoBattleSceneSpeed = (_startPosVec3.z - _normalVec3.z) / _intoBattleSceneTime;
        _intoBattleScenemFinishTime = BattleMgr.Instance.Timer + _intoBattleSceneTime;
        _intoBattleScene = true;
        _intoSceneLoad = true;
        BattleCamera.transform.position = _normalVec3;
    }

    public void StartFollow(Vector3 startPos, float moveSpeed, bool goForward,float finishTime = -1)
    {
        return;
    }

 
    public float LookAtTargetTroop(int targetIndex)
    {
        return 0;
    }

    public float _moveToNormalEffectSpeed = 20;

    public Vector3 _moveToNormalEffectTarPos;

    public Vector3 _moveToNormalEffectDir;

    public float _moveToNormalEffectFinishTime;

    public float MoveToNormalEffectPos(int doActionTroopID, int targtTroopID)
    {
        return 0;
    }

    public float BackFromTargetTroop()
    {
        return 0;
    }

    public float GetBackFromTargetTroopTime( int targetTroopID)
    {
        return 0;       
    }
   

    public void ComeBack(int troopIndex)
    {
        return;       
    }

    

    public float _lookAtTargetSpeed = 20f;
    public float _lookAtTargetFinishTime;
    public Vector3 _lookAtTargetVec3;
    public Vector3 _lookAtTargetPos;


    public float _cameraBackToFixedSpeed = 10f;
    public float _cameraBackToFixedFinishTime;
    public Vector3 _canmeraBackToFixedDirVec3;

    public void OnChangeBattleCameraState()
    {

    }

    public void StopFollow()
    {

    }

    void Update()
    {
        if (!BattleStage.g_InBattle)
        {
            return;
        }

        if (!_intoSceneLoad) 
        {
            return;
        }

        if (BattleCamera == null)
        {
            return;
        }

        if (_intoBattleScene)
        {
            if (BattleMgr.Instance.Timer >= _intoBattleScenemFinishTime)
            {
                _intoBattleScene = false;
                BattleMgr.Instance.OnStartBattle();
                BattleMgr.Instance.PlayFragment();
            }
            return;
        }

        CameraMove();
    }

    void OnGUI() 
    {
        if (GUI.Button(new Rect(0f, Screen.height * 0.1f, 100f, 100f),(_cameraMove ? "关闭摄像机移动" : "开启摄像机移动"))) 
        {
            bCameraMove = !bCameraMove;
        }
    }

    void CameraMove() 
    {
        if (!_cameraMove)
            return;

        if (!InputUtil.IsPress)
            return;

        if (InputUtil.IsTouchDown)
        {
            _lastMouse = Input.mousePosition;
        }

        if (InputUtil.IsTouch)
        {
            _deltaMouse = Input.mousePosition - _lastMouse;
            _lastMouse = Input.mousePosition;
            //print(_deltaMouse);
            _deltaMove = _deltaMouse * _moveSpeed * RealTime.deltaTime;
            if (_deltaMove.x + BattleCamera.transform.position.z > maxX)
                _deltaMove.x = maxX - BattleCamera.transform.position.z;

            if (_deltaMove.x + BattleCamera.transform.position.z < minX)
                _deltaMove.x = minX - BattleCamera.transform.position.z;

            if (_deltaMove.y + BattleCamera.transform.position.y > maxZ)
                _deltaMove.y = maxZ - BattleCamera.transform.position.y;

            if (_deltaMove.y + BattleCamera.transform.position.y < minZ)
                _deltaMove.y = minZ - BattleCamera.transform.position.y;

            BattleCamera.transform.Translate(0, _deltaMove.y, _deltaMove.x, Space.World);
        }
    }
}
